from pydantic_settings import BaseSettings
from pydantic import ValidationError
from typing import Literal
from functools import lru_cache
from pathlib import Path

"""配置文件
"""

class Settings(BaseSettings):
    MODE: Literal["dev", "pro", "test"] = "dev"  # test模式读取本地视频
    BASE_DIR: str = str(Path(__file__).parent.parent.absolute())
    DATA_RESULT_KEY: str = "phd_w_data_results"

    # EMAIL
    ENABLED_EMAIL: bool = False # 是否启用邮件
    EMAIL_PASSWORD: str = 'G@9742ns'
    @property
    def SERVER_EMAIL(self) -> str:
        """发件人"""
        if self.MODE == "pro":
            return "no-reply@gnstec.com.hk"
        return "test@gnstec.com.hk"

    TO_ADDR: str = 'feng@gnstec.com.hk'     # 收件人
    SMTP_SERVER: str = 'smtp.office365.com'  
    SMTP_PORT: int = 587  

    # 检测
    ENABLED_PROCESS_TIME: bool = False       # 是否显示处理时间
    ENABLED_CHECK_WINDOWS: bool = False     # 是否启用窗口检测
    ENABLED_CHECK_CURSOR: bool = False      # 鼠标检测
    MOUSE_COLOR: Literal["yellow","black", "white", "test"] = "white" # 鼠标颜色
    ENABLED_TATE_TIME_OCR: bool = False     # 检测时间ocr
    ENABLED_HEART_BEAT: bool = False        # 是否开启心跳
    HEART_INTERVAL: int = 10                # 心跳间隔
    VIDEO_RECORDER: bool = False            # 屏幕录制
    ENABLED_CHECK_LAYOUT: bool = False      # 是否启用布局检测

    PEAK_START_TIME: str = "05:00"          # 高峰期开始时间
    PEAK_END_TIME: str = "00:30"            # 高峰期结束时间
    PEAK_FREEZE_THRESHOLD: int = 15         # 高峰期冻结阈值(分钟)
    NORMAL_FREEZE_THRESHOLD: int = 120      # 非高峰期冻结阈值(分钟)
    ENABLED_CHECK_FREEZE: bool = True       # 是否启用冻结检测

    # websocket
    WEBSOCKET_PORT: int = 5000
    WEBSOCKET_HOST: str = "0.0.0.0"

    # redis
    REDIS_HOST: str = "127.0.0.1"
    REDIS_DB: int = 0
    REDIS_PORT: int = 6379
    GET_DATA_FREQUENCY: float = 0.3         # 从redis中获取数据的频率

    # BASE
    SHOW_WINDOW_NAME: str = "Kowloon Bay Depot Smart Watch Dog" # cv2.show的名字
    SHOW_SIZE: tuple = (1266, 670)

    # TEST
    TEST_VIODE_PATH: str = ""

    # 文字显示的位置
    POSITION_X: int = 600
    POSITION_Y: int = 100


    class Config:
        env_file = "settings.conf"

@lru_cache
def get_settings():
    try:
        return Settings()
    except ValidationError as e:
        msg = e.errors()
        error_message = f"\033[91menvironment variable error: \n [{msg[0]['loc'][0].upper()}={msg[0]['input']}]-[{msg[0]['msg']}]\033[0m"
        raise ValueError(error_message)
